/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import connection.AbstractConnection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Ricardo
 */
public abstract class AbstractDAO {
    protected AbstractConnection conn;
    
    protected final String sqlGetMaxId = "select max() from ";
    
    public AbstractDAO(AbstractConnection conn) {
        this.conn = conn;                
    }
    
    protected int getNextId(String table,String field) throws SQLException {
        StringBuilder sb = new StringBuilder(sqlGetMaxId);
        sb.insert(11, field);
        sb.append(table);
        
        PreparedStatement ps = conn.getConnection().prepareStatement(field);
        ResultSet rs = ps.executeQuery();
        
        return rs.getInt(1);
    }
    
    protected AbstractConnection getNewConnection() {
        try {
            return conn.getClass().newInstance();
        } catch (IllegalAccessException e) {
            return null;
        } catch (InstantiationException e) {
            return null;
        }
    }
    
}
